<?php
header('Content-Type: text/html; charset=utf-8');

include "configuration.php";
require_once MODELS_DIR . "DataLayer.php";
require_once MODELS_DIR . "ProcessingDate.php";
require_once LIBRARIES_DIR . "Mysql.php";
require_once LIBRARIES_DIR . "Util.php";

$dir = opendir(CONTROLLERS_DIR);

while( $classe = readdir($dir) )
{
	if(preg_match("'^.*\.php'", $classe))
	{
		require_once(CONTROLLERS_DIR.$classe);	
	}
}
closedir($dir);
session_start();

$db_Mysql = new Mysql(HOST_DB_MYSQL, USER_DB_MYSQL, PASS_DB_MYSQL, NAME_DB_MYSQL, false) or exit ("Não foi possível conectar ao banco de dados!");

$pgLogin		= strstr($_SERVER['PHP_SELF'], "content/login");
$pgLoginAjax	= strstr($_SERVER['PHP_SELF'], "app/Views/V_User.php");

if($_SESSION["USER"] == NULL || empty($_SESSION["USER"])){
	$typeSessionUser = NULL;
}else{
	$typeSessionUser = get_class($_SESSION["USER"]);
}

/*CONTROLA A SESSION DO USUARIO*/
//Chamada pelo Ajax
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
	if($typeSessionUser != "User" && $pgLoginAjax == ""){
		echo json_encode(array(false,"Sessão expirada!","location = '".SITE."content/login/'"));
		exit();
	}
//Chamada pelo HTTP
}else{
	if($typeSessionUser != "User" && $pgLogin == ""){
		header("Location: ".SITE."content/login/");
	}else if($pgLogin != "" && $typeSessionUser == "User"){
		header("Location: ".SITE."content/project/");
	}
}


/*SETA O PROJETO EM USO*/
$idProject = (int)$_GET['project'];
if($idProject > 0){
	if($_SESSION["PROJECT"] == NULL || empty($_SESSION["PROJECT"])){
		$idCurrentProject = 0;
	}else{
		if(get_class($_SESSION["PROJECT"]) == "Project")
			$idCurrentProject = $_SESSION["PROJECT"]->id;
	}

	if($idCurrentProject != $idProject){
		$objProjectDAO = new ProjectDAO($db_Mysql);
		$resultProject = $objProjectDAO->getProjects($idProject,0,array(1,2,3));

		if($resultProject[0] > 0)
			$_SESSION["PROJECT"] = $resultProject[0];

		//Scrum master, só acessa os projetos onde foi atribuido.
		if($_SESSION["USER"]->idGroup == 2 && $_SESSION["USER"]->id != $_SESSION["PROJECT"]->idScrumMaster){
			$_SESSION["USER_PERMISSION"] = 3;
		}else{
			$_SESSION["USER_PERMISSION"] = $_SESSION["USER"]->idGroup;
		}
	}
}

//Verifca o acesso por grupo nas sessões do site
$sectionGroups["project"]	= NULL;
$sectionGroups["sprint"]	= NULL;
$sectionGroups["kanban"]	= NULL;
$sectionGroups["team"]		= array(1,2);
$sectionGroups["meeting"]	= array(1,2);
$sectionGroups["user"]		= array(1);
$sectionGroups["calendar"]	= NULL;
$sectionGroups["graphic"]	= NULL;
$sectionGroups["config"]	= NULL;

if(empty($_SESSION["USER_PERMISSION"]))
	$_SESSION["USER_PERMISSION"] = $_SESSION["USER"]->idGroup;

if($_SESSION["USER"] != NULL && !empty($_SESSION["USER"])){
	if(count($sectionGroups[SECTION]) > 0){	
		if(!in_array($_SESSION["USER_PERMISSION"],$sectionGroups[SECTION]))
			header("Location: ".SITE."content/project/");
	}		
}

?>